<!DOCTYPE html>
<html lang="ch-zn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="./node_modules/bootstrap-icons/font/bootstrap-icons.css">
    <title>还书页面</title>
    <script src="./node_modules/bootstrap/dist/js/bootstrap.bundle.js"></script>
    <style>

    </style>
</head>

<body>
    <div class="container">
        <nav class="navbar navbar-expand-lg bg-body-tertiary">
            <div class="container-fluid">
                <a class="navbar-brand" href="./index.html">图书管理系统</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">图书管理</a>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" href="./bookquery.html">图书查询</a></li>
                                <li><a class="dropdown-item" href="./bookchange.html">图书更改</a></li>
                            </ul>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">学生入口</a>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" href="./signup.html">新生注册</a></li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li><a class="dropdown-item" href="./borrowbook.html">我要借书</a></li>
                                <li><a class="dropdown-item" href="./returnbook.html">我要还书</a></li>
                            </ul>
                        </li>
                    </ul>
                    <form class="d-flex" role="search">
                        <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                        <button class="btn btn-outline-success" type="submit">Search</button>
                    </form>

                </div>

            </div>
        </nav><br/>
        <div class="row">
            <div class="input-group mb-3">
                <span class="input-group-text">学生证号</span>
                <input type="text" class="form-control" placeholder="Id number" id="sid" aria-label="Id" aria-describedby="basic-addon1">
            </div>
        </div>
        <div class="row">
            <div class="input-group mb-3">
                <span class="input-group-text" id="basic-addon1">书籍编号</span>
                <input type="text" class="form-control" placeholder="Bookid" aria-label="Bookid" id="bookid" aria-describedby="basic-addon1">
            </div>
            <div class="row">
                <div class="col"></div>
                <button type="button" class="btn btn-primary col-sm-3 " onclick="returnBook()">还书</button>
                <div class="col"></div>
                <div class="col"></div>
                <button type="button" class="btn btn-primary  col-sm-3 " onclick="queryBorrow()">查询借书记录</button>
                <div class="col"></div>
                <div class="col"></div>
                <button type="button" class="btn btn-primary  col-sm-3 " onclick="payForBook()">为本书缴费</button>
                <div class="col"></div>
            </div>
            <div id="output">

            </div>
        </div>
</body>
<script type="text/javascript" src="./node_modules/jquery/dist/jquery.js" charset="UTF-8"></script>
<script>
    function returnBook() {
        var input_stuid = document.getElementById("sid").value;
        var input_bookid = document.getElementById("bookid").value;
        if (input_stuid == "" | input_bookid == "") {
            alert("学号和书籍号不得为空！");
            return;
        }
        inputdata = {
            "input_stuid": input_stuid,
            "input_bookid": input_bookid
        }
        var url0 = 'http://localhost:80/index.php/Mysql/'
        $.ajax({
            methods: "GET",
            url: url0 + "returnBookById/",
            async: pageYOffset,
            data: inputdata,
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            success: function(data) {
                if (data == "success") {
                    alert("还书成功！")
                }
                if (data == "没有查阅到借阅信息") {
                    alert('没有查阅到该书的借阅信息');
                }
                if (data.length > 12 && data.slice(0, 10) == "date error") {
                    alert("您这本书书借阅时长超过" + data.slice(11) + "天，需要缴纳" + data.slice(11) * 0.02 + "元,请前去缴费！");
                }
            },
            error: function(err) {
                // 请求失败， err是失败的内容
                print("请求失败");
            },
            complete: function(e) {
                // 不管成功还是失败都会调用这个函数
            }
        })
    }

    function queryBorrow() {
        var input_stuid = document.getElementById("sid").value;
        if (input_stuid == "") {
            alert("学号不得为空！");
            return;
        }
        var url0 = 'http://localhost:80/index.php/Mysql/'
        $.ajax({
            methods: "GET",
            url: url0 + "queryBorrow/input_stuid/" + input_stuid,
            async: pageYOffset,
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            success: function(data) {
                data_np = Object.values(data);
                console.log(data_np.length);
                console.log(typeof(data_np.length));
                //找到要输出的位置
                var output = document.getElementById('output');
                //初始化输出文本
                var value_np_out = "";
                value_np_out += "<table class='table'>" + "<thead>" + "<tr>" + "<th scope='col'>姓名</th><th scope='col'>图书编号</th><th scope='col'>图书名称</th><th scope='col'>借阅时间（每年中的天为单位）</th><th scope='col'>借阅数量</th>" + "</tr>" + "</thead>" + "<tbody > "
                for (i1 = 0; i1 < data_np.length; i1++) {
                    var value_np = Object.values(data_np[i1])
                    console.log(value_np);
                    value_np_out += "<tr>"
                    for (i2 = 0; i2 < value_np.length; i2++) {
                        value_np_out += "<td>" + value_np[i2] + "</td>";
                    }
                    value_np_out += "</tr>"
                    console.log(value_np_out);
                }
                value_np_out += "</tbody><table>"
                output.innerHTML = value_np_out;
            },
            error: function(err) {
                // 请求失败， err是失败的内容
                print("请求失败");
            },
            complete: function(e) {
                // 不管成功还是失败都会调用这个函数
            }
        })
    }

    function payForBook() {
        var input_stuid = document.getElementById("sid").value;
        var input_bookid = document.getElementById("bookid").value;
        inputdata = {
            "input_stuid": input_stuid,
            "input_bookid": input_bookid
        }
        if (input_stuid == "" | input_bookid == "") {
            alert("学号和书籍号不得为空！");
            return;
        }
        var url0 = 'http://localhost:80/index.php/Mysql/'
        $.ajax({
            methods: "GET",
            url: url0 + "payForBook/",
            async: pageYOffset,
            data: inputdata,
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            success: function(data) {
                alert(data);

            },
            error: function(err) {
                // 请求失败， err是失败的内容
                print("请求失败");
            },
            complete: function(e) {
                // 不管成功还是失败都会调用这个函数
            }
        })
    }
</script>

</html>